﻿(function($, w) {
	 
	/**
	 *打开新页面 
	 * @param {Object} path
	 */
	w.open_webview = function(path, extras) {
			mui.openWindow({
				url: path,
				extras: extras || {}
			});
		}
		/**
		 *  网络请求
		 * @param {Object} path
		 * @param {Object} data
		 * @param {Object} callback
		 */
	w.ajax = function(path, method, data, callback, issw) {
	 
		console.log(data)
			var options = {
				path: path,
				method: method,
				data: data,
				callback: callback,
				issw: issw
			}
//			if(issw != false) {
//				issw = true;
//			}
//			if(issw) {
//				plus.nativeUI.showWaiting();
//			}
			$.ajax(w.HOST + path, {
				data: data || {},
				dataType: 'json', //服务器返回json格式数据
				type: method, //HTTP请求类型
				timeout: 5000, //超时时间设置为5秒；
				success: function(data) {
					if(issw) {
//						plus.nativeUI.closeWaiting();
					}
					if(data && data.status == 0) {
						if(data.errno == 1011 && path != 'login') {
							autoLogin(options);
						} else {
							return callback(data.responseText);
						}
					} else {
						return callback(null, data.data);
					}
				},
				error: function(xhr, type, errorThrown) {
					if(issw) {
//						plus.nativeUI.closeWaiting();
					}
					return callback("网络状态不佳");
				}
			});
		}
	w.getToken = function(callback) {
			w.ajax('upload/token', 'get', null, function(err, data) {
				if(err) {
					console.log(err);
					callback(null);
				} else {
					callback(data);
				}
			});
		}
		// 空函数
	function shield() {
		return false;
	}
	document.addEventListener('touchstart', shield, false); //取消浏览器的所有事件，使得active的样式在手机上正常生效
	document.oncontextmenu = shield; //屏蔽选择函数

	// DOMContentLoaded事件处理
	var domready = false;
	document.addEventListener('DOMContentLoaded', function() {
		mui.plusReady(function() {
			plus.nativeUI.closeWaiting();
		});
		domready = true;
		document.body.onselectstart = shield;
		compatibleAdjust();
	}, false);
	// 清除预创建页面(仅)
	function preateClear() {
		for(var p in preate) {
			var w = preate[p];
			if(w && w.showded && !w.isVisible()) {
				w.close();
				preate[p] = null;
			}
		}
	}
 
		/**
		 *点击打开页面  初始化点击事件 
		 */
	w.initTap = function(className) {
			var cell = document.getElementsByClassName(className);
			for(var i = 0; i < cell.length; i++) {
				cell[i].addEventListener('tap', function() {
					var link = this.getAttribute('link');
					if(link) {
						w.open_webview(link + '.html');
					}

				});
			}
		}
		/**
		 *点击打开页面  初始化点击事件 
		 */
	w.initTapAndData = function(className) {
			var cell = document.getElementsByClassName(className);
			for(var i = 0; i < cell.length; i++) {
				cell[i].addEventListener('tap', function() {
					var link = this.getAttribute('link');
					var path = this.getAttribute('path') || "";
					var type = this.getAttribute('type') || 0;
					var title = this.getAttribute('title') ? this.getAttribute('title') : this.innerText;
					if(link && title) {
						mui.openWindow({
							url: link + '.html',
							id: link + '.html',
							extras: {
								path: path,
								title: title.trim(),
								type: type
							}
						});
					}

				});
			}
		}
		 
})(mui, window.cmd = {});